#ifndef COMBINER_H
#define COMBINER_H

#include "number_source.h"

using namespace std;

template <typename T, typename Distance = ptrdiff_t>
class combiner : public iterator<T, Distance>
{
public:
  void read()
  {
    ++source1;
    ++source2;
    value = func(*source1, *source2);
  }

  friend bool
  operator== (const combiner<T, Distance>& x,
	      const combiner<T, Distance>& y)
  {
    return (x.source1 == y.source1) && (y.source2 == x.source2);
  }

  combiner() 
  : source1(), source2()
  { }

  combiner(iterator<T, Distance>& s1,
	   iterator<T, Distance>& s2,
	   binary_function<T, T, T> f)
    : source1(s1), source2(s2), func(f)
  { }

  combiner<T, Distance>&
  operator++()
  {
    read();
    return *this;
  }

  combiner<T, Distance>&
  operator++(int)
  {
    combiner<T, Distance>& tmp = *this;
    read();
    return tmp;
  }

  const T&
  operator*() const
  {
    return value;
  }

private:
  T value;
  number_source<T, Distance> source1;
  number_source<T, Distance> source2;
  binary_function<T, T, T> func;
};

#endif

// Local Variables:
// mode: C++
// End:
